<?php

namespace Common\Model;

use Think\Model;

class AbilityCheckModel extends Model {

    /**
     * 个人能力报告信息
     * @param int $iAbilityCheckId 测评报告ID
     * @param int $iFromType 查看测评角色  1-个人 2-企业
     * @param int $iRelatId 相关ID 如果$iFromType=1则$iRelatId为iUserId; 如果$iFromType=2则$iRelatId为iCompanyId。
     * @return array 
     */
    public function abilityCheckReportInfo($iAbilityCheckId, $iFromType, $iRelatId) {
        //个人基本信息
        if ($iFromType == 1) {
            $arrWhere['ac.user_id'] = $iRelatId;
            $arrWhere['ac.read_auth'] = array('exp', '& 1');
        } else {
            $arrWhere['ac.company_id'] = $iRelatId;
            $arrWhere['ac.read_auth'] = array('exp', '& 2');
        }
        $arrWhere['ac.ability_check_id'] = $iAbilityCheckId;
        $userInfo = $this->table('qy_ability_check ac')
                ->field("ac.ability_check_id,u.birthday,u.sex,ac.school,ac.education,ac.major,DATE_FORMAT(ac.create_time,'%Y-%m-%d') as create_time,ac.cate_id,ac.check_level,u.real_name,u.phone,ca.cate_name")
                ->join('qy_user as u USING(user_id)')
                ->join('qy_category_all as ca USING(cate_id)')
                ->where($arrWhere)
//                ->cache(TRUE)
                ->find();
        
        if (empty($userInfo)) {
            return $userInfo;
        }
        $userInfo['check_level_name'] = abilityLevelName($userInfo['check_level']);
        $temp_check_level = $userInfo['check_level'] == 0 ? 1 : $userInfo['check_level']; //新手获取初级的二层调用
        $retrunList['user_info'] = $userInfo;

        //二层能力信息
        $secondAnswerList = $this->table('qy_ability_check_answer aca')
                ->field('acq.second_ability_level_id, COUNT(acq.second_ability_level_id) as answer_total, SUM(aca.check_result) as right_count')
                ->join('qy_ability_check_question as acq USING(ability_check_question_id)')
                ->where(array('aca.ability_check_id' => $iAbilityCheckId))
                ->group('acq.second_ability_level_id')
                ->select();

        if (empty($secondAnswerList)) {
            return $retrunList;
        }

        $secondAbilityLevelIds = array();
        foreach ($secondAnswerList as $val) {
            $secondAbilityLevelIds[] = $val['second_ability_level_id'];
        }
        $abilityLevelList = $this->table('qy_ability_level')
                ->field('ability_level_id,ability_name,description,content')
                ->where(array('ability_level_id' => array('in', $secondAbilityLevelIds)))
                ->order('ability_level_id ASC')
                ->select();

        foreach ($abilityLevelList as &$val) {
            foreach ($secondAnswerList as $vv) {
                if ($val['ability_level_id'] == $vv['second_ability_level_id']) {
                    $val = array_merge($val, $vv);
                    $val['check_score'] = (number_format($vv['right_count'] / $vv['answer_total'], 3) * 100);
                }
            }
        }
        unset($val);
        $retrunList['second_ability_list'] = $abilityLevelList;

        //掌握、熟悉、了解、薄弱 列表信息
        $mature['zw'] = array();
        $mature['sx'] = array();
        $mature['lj'] = array();
        $mature['br'] = array();
        foreach ($abilityLevelList as $val) {
            $check_score = $val['check_score'];
            switch (true) {
                case ($check_score >= 80):
                    $mature['zw'][] = $val['description'];
                    break;
                case ($check_score >= 60 && $check_score < 80):
                    $mature['sx'][] = $val['description'];
                    break;
                case ($check_score >= 40 && $check_score < 60):
                    $mature['lj'][] = $val['description'];
                    break;
                default:
                    $mature['br'][] = $val['description'];
                    break;
            }
        }

        foreach ($mature as &$val) {
            $val = implode('，', $val);
        }
        unset($val);
        $retrunList['mature_list'] = $mature;

        //三层能力信息
        $thirdAnswerList = $this->table('qy_ability_check_answer aca')
                ->field('acq.third_ability_level_id, COUNT(acq.third_ability_level_id) as answer_total, SUM(aca.check_result) as right_count, al.ability_name')
                ->join('qy_ability_check_question as acq USING(ability_check_question_id)')
                ->join('qy_ability_level as al ON al.ability_level_id = acq.third_ability_level_id')
                ->where(array('aca.ability_check_id' => $iAbilityCheckId))
                ->group('acq.third_ability_level_id')
                ->select();

        if (empty($thirdAnswerList)) {
            return $retrunList;
        }
        //初始化三层能力值
        $arrGourpThirdAnswerList = array(
            'best' => array('count' => 0),
            'average' => array('count' => 0),
            'bad' => array('count' => 0)
        );
        $iBestCount = 0;
        $iAverageCount = 0;
        $ibadCount = 0;
        foreach ($thirdAnswerList as $val) {
            $check_score = (number_format($val['right_count'] / $val['answer_total'], 2) * 100);
            if ($check_score >= 75) {
                $arrGourpThirdAnswerList['best']['ability_name'][] = $val['ability_name'];
                $arrGourpThirdAnswerList['best']['count'] = ++$iBestCount;
                ;
            } elseif ($check_score < 75 && $check_score >= 33) {
                $arrGourpThirdAnswerList['average']['ability_name'][] = $val['ability_name'];
                $arrGourpThirdAnswerList['average']['count'] = ++$iAverageCount;
            } else {
                $arrGourpThirdAnswerList['bad']['ability_name'][] = $val['ability_name'];
                $arrGourpThirdAnswerList['bad']['count'] = ++$ibadCount;
            }
        }

        $retrunList['third_ability_list'] = $arrGourpThirdAnswerList;
        return $retrunList;
    }

}
